define(['app'], function(app) {
    'use strict';

    var controller = function controller($scope, Newgive, $window, $timeout, $ionicSlideBoxDelegate, GoPage,$interval) {
       
        //画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			$scope.lan = 0; //这是状态栏，只有ios的APP才有
			if(window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) {
				$scope.lan = 20;
			}
			$scope.all = $window.innerHeight;
			$scope.data_init();
		}, false);
		
		$scope.$on('$ionicView.beforeLeave',function(){
			$interval.cancel($scope.timer);
		})
		
      	$scope.selectIndex = 0;
       	//切换页面
		$scope.activeSlide = function(index) {
			$scope.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide($scope.selectIndex);
		};
		$scope.slideChanged = function(index) {
			$scope.selectIndex = index;
		};
		
		//禁止左右滑动来切换页面1
		$scope.onTouch = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
		};
		//禁止左右滑动来切换页面2
		$scope.onRelease = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(true);
		};
		
		//初始化页面
		$scope.data_init = function() {
			Newgive.get_test_list(1, 'started', function(data, now_time) {
				if(data != null) {
					$scope.init_time(data, now_time);
					$scope.doing = data;
					$scope.set_timer(now_time,data.act);
				} else {
					$scope.doing = new Object();
				}
				$scope.doing_nPage = 1;
			});
			Newgive.get_test_list(1, 'will', function(data, now_time) {
				if(data != null) {
					$scope.init_time(data, now_time);
					$scope.will = data;
				} else {
					$scope.will = new Object();
				}
				$scope.will_nPage = 1;
			});
			Newgive.get_test_list(1, 'ended', function(data, now_time) {
				if(data != null) {
					$scope.init_time(data, now_time);
					$scope.ended = data;
				} else {
					$scope.ended = new Object();
				}
				$scope.ended_nPage = 1;
			});
			Newgive.get_test_list(1, '', function(data, now_time) {
				if(data != null) {
					$scope.init_time(data, now_time);
					$scope.datas = data;
					$scope.set_timer(now_time,data.act);
				} else {
					$scope.datas = new Object();
				}
				$scope.all_nPage = 1;
			});
			//已申请
			Newgive.get_test_list(1, 'apply', function(data, now_time) {
				$scope.set_timer(now_time);
				if(data != null) {
					$scope.init_time(data, now_time);
					$scope.apply = data;
					$scope.set_timer(now_time,data.act);
				} else {
					$scope.apply = new Object();
				}
				$scope.apply_nPage = 1;
			});
			
			//轮播图
			Newgive.getBannerList(function(data){
				if(!data.result) return;
				$scope.banners = data.result.associate;
				$ionicSlideBoxDelegate.$getByHandle('advs-handle').update(); //解决无法加载
				$ionicSlideBoxDelegate.$getByHandle('advs-handle').loop(true); //循环
				$scope.getAdvsBox();
			})
		}
		
		//获取轮播图的高度。获取到的高度一般是最高的那张图片的高度，如果接口比较慢，得到的很可能是最矮的那张
		$scope.getAdvsBox = function(){
			$timeout(function(){
				$scope.advsBox = document.getElementById("advsBox").clientHeight;
				if($scope.banners==null||$scope.banners.length==0||$scope.advsBox){//没有轮播图或已经获取到了高度，则取消定时器
					$interval.cancel($scope.getAdvsBox);
				}
				$scope.myObj = {
					"height": $scope.all - 44*2 - $scope.advsBox - $scope.lan + "px"
				}
			},500)
		}
		
		/******************时间处理**********************/
		$scope.init_time = function(data, now_time) {
			for(var i = 0; i < data.act.length; i++) {
				if(data.act[i].now_time != undefined)
					data.act[i].now_time = data.act[i].now_time - 1;
				else{
					if(data.act_status==1){
						data.act[i].now_time = data.act[i].start_time - now_time;
					}else{
						data.act[i].now_time = data.act[i].end_time - now_time;
					}
				}
				data.act[i].formate_time = $scope.formate_time(data.act[i].now_time);
			}
		}
		$scope.set_timer = function(now_time,dataArr) {
		  	$scope.timer = $interval(function() {
				if(dataArr){
					for(var i = 0; i < dataArr.length; i++) {
						if(dataArr[i].now_time != undefined)
							dataArr[i].now_time = dataArr[i].now_time - 1;
						else
							dataArr[i].now_time = dataArr[i].end_time - now_time;
						dataArr[i].formate_time = $scope.formate_time(dataArr[i].now_time);
					}
				}
			}, 1000)
		}
		$scope.formate_time = function(time) {
			if(time > 0) {
				var day = parseInt(time / (3600 * 24));
				var hour = parseInt((time % (3600 * 24)) / 3600);
				if(hour < 10)
					hour = "0" + hour;
				var min = parseInt((time % 3600) / 60);
				if(min < 10)
					min = "0" + min;
				var second = time % 60;
				if(second < 10)
					second = "0" + second;
				return day + '天' + hour + '小时' + min + '分' + second + '秒';
				console.log(1);
			} else {
				return '0天0小时0分0秒';
			}
		}
		/******************时间处理**********************/
		
		
		/*****************上下拉刷新*****************/
		$scope.isLoad = false;
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			if($scope.selectIndex == 0) {//进行中
				Newgive.get_test_list(1, 'started', function(data, now_time) {
					if(data != null) {
						$scope.init_time(data, now_time);
						$scope.doing = data;
						$scope.set_timer(now_time,data.act);
					} else{
						$scope.doing = new Object();
					}
					$scope.doing_nPage = 1;
				}, function() {
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1000);
				})
			} 
			if($scope.selectIndex == 1) {//即将开始
				Newgive.get_test_list(1, 'will', function(data, now_time) {
					if(data != null) {
						$scope.init_time(data, now_time);
						$scope.will = data;
					} else {
						$scope.will = new Object();
					}
					$scope.will_nPage = 1;
				}, function() {
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1000);
				})
			}
			if($scope.selectIndex == 2){//已结束
				Newgive.get_test_list(1, 'ended', function(data, now_time) {
					if(data != null) {
						$scope.init_time(data, now_time);
						$scope.ended = data;
					} else
						$scope.ended = new Object();
					$scope.ended_nPage = 1;

				}, function() {
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1000);
				})
			}
			if($scope.selectIndex == 3) {//全部
				Newgive.get_test_list(1, '', function(data, now_time) {
					if(data != null) {
						$scope.init_time(data, now_time);
						$scope.datas = data;
						$scope.set_timer(now_time,data.act);
					} else
						$scope.datas = new Object();
						$scope.all_nPage = 1;
				}, function() {
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1000);
				})
			}
			if($scope.selectIndex == 4) {//已申请
				Newgive.get_test_list(1, 'apply', function(data, now_time) {
					if(data != null) {
						$scope.init_time(data, now_time);
						$scope.apply = data;
						$scope.set_timer(now_time,data.act);
					} else
						$scope.apply = new Object();
						$scope.apply_nPage = 1;
				}, function() {
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1000);
				})
			}
			
		} 
		
		$scope.doRefreshMore = function() {
			$scope.isLoad = true;
			if($scope.selectIndex == 0) {//进行中
				if($scope.doing.page.count <= $scope.doing.act.length) {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1000);
					return;
				}
				Newgive.get_test_list($scope.doing_nPage + 1, 'started', function(data, now_time) {
					if(data != null) {
						if($scope.doing.act != undefined) {
							$scope.init_time(data, now_time);
							$scope.doing.act = $scope.doing.act.concat(data.act);
							$scope.doing_nPage += 1;
						} else {
							$scope.doing = data;
							$scope.doing_nPage = 1;
						}
					}
				}, function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1000);
				})
			} 
			if($scope.selectIndex == 1) {//即将开始
				if($scope.will.page.count <= $scope.will.act.length) {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1000);
					return;
				}
				Newgive.get_test_list($scope.will_nPage + 1, 'will', function(data, now_time) {
					if(data != null) {
						if($scope.will.act != undefined) {
							$scope.init_time(data, now_time);
							$scope.will.act = $scope.will.act.concat(data.act);
							$scope.will_nPage += 1;
						} else {
							$scope.will = data;
							$scope.will_nPage = 1;
						}
					}
				}, function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1000);
				})
			}
			if($scope.selectIndex == 2){//已结束
				if($scope.ended.page.count <= $scope.ended.act.length) {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1000);
					return;
				}
				Newgive.get_test_list($scope.ended_nPage + 1, 'ended', function(data, now_time) {
					if(data != null) {
						if($scope.datas.act != undefined) {
							$scope.init_time(data, now_time);
							$scope.ended.act = $scope.ended.act.concat(data.act);
							$scope.ended_nPage += 1;
						} else {
							$scope.ended = data;
							$scope.ended_nPage = 1;
						}
					}
				}, function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1000);
				})
			}
			if($scope.selectIndex == 3) {//全部
				if($scope.datas.page.count <= $scope.datas.act.length) {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1000);
					return;
				}
				Newgive.get_test_list($scope.all_nPage + 1, '', function(data, now_time) {
					if(data != null) {
						if($scope.datas.act != undefined) {
							$scope.init_time(data, now_time);
							$scope.datas.act = $scope.datas.act.concat(data.act);
							$scope.all_nPage += 1;
						} else {
							$scope.datas = data;
							$scope.all_nPage = 1;
						}
					}
				}, function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1000);
				})
			}
			if($scope.selectIndex == 4) {//已申请
				if($scope.datas.page.count <= $scope.apply.act.length) {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1000);
					return;
				}
				Newgive.get_test_list($scope.all_nPage + 1, 'apply', function(data, now_time) {
					if(data != null) {
						if($scope.apply.act != undefined) {
							$scope.init_time(data, now_time);
							$scope.apply.act = $scope.apply.act.concat(data.act);
							$scope.apply_nPage += 1;
						} else {
							$scope.apply = data;
							$scope.apply_nPage = 1;
						}
					}
				}, function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1000);
				})
			}
			
		}
		
		//页面跳转
		$scope.onshow_detail= function(act_id,act_type,goods_id,product_id) {
			GoPage.go_states("newsend_detail", {
				"act_id": act_id,
				"goods_id":goods_id,
				"product_id":product_id
			});
		};
		$scope.onshow_first = function(store_id,$event){
			$event.stopPropagation();//阻止事件冒泡
			GoPage.go_states("first", {
				'store_id': store_id
			});
		}


    }

    controller.$inject = ['$scope', 'Newgive', '$window', '$timeout', '$ionicSlideBoxDelegate', 'GoPage','$interval'];

    return app.registerController("newgivectrl",controller);
});